package com.zk.babala.datasource;

import android.widget.Toast;

import com.zk.babala.BabalaApp;
import com.zk.babala.entity.CategoryInfo;
import com.zk.babala.entity.LineInfo;
import com.zk.babala.entity.Movie;
import com.zk.babala.entity.PlayNumInfo;
import com.zk.babala.entity.VideoInfo;
import com.zk.babala.service.ISourceParse;
import com.zk.babala.util.LineUtil;
import com.zk.babala.util.RequestUtil;

import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hutool.http.HtmlUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

/**
 * 云影
 */
public class YunySource implements ISourceParse {
    public String nextUrl;

    @Override
    public Map<String, String> getSourceMeta() {
        Map<String, String> map = new HashMap<>();
        map.put("source", this.getClass().getSimpleName());
        map.put("name", "云影");
        map.put("url", "https://www.yuny.tv/");
        return map;
    }

    public YunySource() {
    }

    @Override
    public List<CategoryInfo> getCategory() {
        List<CategoryInfo> categoryInfoList = new ArrayList<>();
        CategoryInfo dj = new CategoryInfo("动漫", "videoClassify/3");
        dj.getChildList().add(new CategoryInfo("国产", "videoClassify/3?subType=国产动漫"));
        dj.getChildList().add(new CategoryInfo("日本", "videoClassify/3?subType=日本动漫"));
        dj.getChildList().add(new CategoryInfo("全部", "videoClassify/3?subType=全部"));
        categoryInfoList.add(dj);
        CategoryInfo dm = new CategoryInfo("短剧", "videoClassify/4");
        dm.getChildList().add(new CategoryInfo("重生", "videoClassify/4?subType=全部&sort=热门&tags=重生"));
        dm.getChildList().add(new CategoryInfo("穿越", "videoClassify/4?subType=全部&sort=热门&tags=穿越"));
        dm.getChildList().add(new CategoryInfo("爱情", "videoClassify/4?subType=全部&sort=热门&tags=爱情"));
        categoryInfoList.add(dm);
        CategoryInfo dy = new CategoryInfo("电影", "videoClassify/1");
        dy.getChildList().add(new CategoryInfo("科幻片", "videoClassify/1?subType=科幻片"));
        dy.getChildList().add(new CategoryInfo("惊悚片", "videoClassify/1?subType=惊悚片"));
        dy.getChildList().add(new CategoryInfo("奇幻片", "videoClassify/1?subType=奇幻片"));
        dy.getChildList().add(new CategoryInfo("日语", "videoClassify/1?subType=全部&current=4&language=日语"));
        CategoryInfo dsj = new CategoryInfo("电视剧", "videoClassify/2");
        categoryInfoList.add(dy);
        dsj.getChildList().add(new CategoryInfo("国产剧", "videoClassify/2?subType=国产剧"));
        dsj.getChildList().add(new CategoryInfo("欧美剧", "videoClassify/2?subType=欧美剧"));
        dsj.getChildList().add(new CategoryInfo("日本剧", "videoClassify/2?subType=日本剧"));
        dsj.getChildList().add(new CategoryInfo("海外剧", "videoClassify/2?subType=海外剧"));
        dsj.getChildList().add(new CategoryInfo("日本综艺", "videoClassify/5?subType=日韩综艺&language=日语"));
        dsj.getChildList().add(new CategoryInfo("韩国综艺", "videoClassify/5?subType=日韩综艺&language=韩语"));
        categoryInfoList.add(dsj);
        return categoryInfoList;
    }

    @Override
    public List<Movie> getMovies(String url) {
        List<Movie> list = new ArrayList<>();
        Document document = RequestUtil.get(url);
        JSONArray dataArray = JSONUtil.parseArray(HtmlUtil.cleanHtmlTag(document.getElementById("__NUXT_DATA__").toString()));
        JSONObject pageData;
        String pageClass = "flex justify-center m-7 gap-3";
        if (url.startsWith(getSearchUrl())) {
            pageClass = "search_result_list_page";
            pageData = dataArray.getJSONObject(dataArray.getJSONObject(4).getInt("data"));
        } else {
            pageData = dataArray.getJSONObject(dataArray.getJSONObject(4).getInt("videoShowList"));
        }

        JSONArray recordList = dataArray.getJSONArray(pageData.getInt("records"));
        //偏移
        int pos = 0;
        for (int i = 0; i < recordList.size(); i++) {
            Object obj = dataArray.get(recordList.getInt(i));
            if (obj instanceof Integer) {
                pos = 1;
                obj = dataArray.get(recordList.getInt(i) - pos);
            }
            if (obj instanceof JSONObject) {
                JSONObject node = (JSONObject) obj;
                Movie m = new Movie();
                m.setTitle(dataArray.getStr(node.getInt("name") - pos));
                m.setVideoUrl(LineUtil.getLineUtil().swapUrl("videoDetail/" + dataArray.getStr(node.getInt("id") - pos)));
                m.setCardImageUrl(dataArray.getStr(node.getInt("cover") - pos));
                list.add(m);
            }

        }

        nextUrl = null;
        Elements pageElement = document.getElementsByClass(pageClass);
        if (!pageElement.isEmpty()) {
            pageElement.get(0).getElementsByTag("a").forEach(page -> {
                if (page.text().equals("下一页")) {
                    nextUrl = LineUtil.getLineUtil().swapUrl(page.attr("href"));
                }
            });
        }
        return list;
    }

    @Override
    public VideoInfo getVideoDetail(String url) {
        VideoInfo videoInfo = new VideoInfo();
        Document document = RequestUtil.get(url);
        Elements infoNodes = document.getElementsByClass("video_appraise_row");
        infoNodes.forEach(infoNode -> {
            videoInfo.getDetailInfoList().add(infoNode.text());
        });

        List<LineInfo> lineInfoList = new ArrayList<>();
        JSONArray dataArray = JSONUtil.parseArray(HtmlUtil.cleanHtmlTag(document.getElementById("__NUXT_DATA__").toString()));
        Object lineObj = dataArray.get((dataArray.getJSONObject(5)).getInt("lines"));
        JSONArray playNode;
        //偏移
        int pos = 0;
        if (lineObj instanceof JSONObject) {
            pos = 1;
            playNode = dataArray.getJSONArray((dataArray.getJSONObject(5)).getInt("lines") - pos);
        } else if (lineObj instanceof JSONObject) {
            playNode = (JSONArray) lineObj;
        } else {
            Toast.makeText(BabalaApp.getContext(), "线路解析失败", Toast.LENGTH_SHORT).show();
            return videoInfo;
        }

        for (int i = 0; i < playNode.size(); i++) {
            JSONObject node = dataArray.getJSONObject(playNode.getInt(i) - pos);
            List<PlayNumInfo> playItemList = new ArrayList<>();
            JSONArray playList = dataArray.getJSONArray(node.getInt("selects") - pos);
            for (int p = 0; p < playList.size(); p++) {
                JSONObject item = dataArray.getJSONObject(playList.getInt(p) - pos);
                PlayNumInfo play = new PlayNumInfo();
                play.setId(p);
                play.setTitle(dataArray.getStr(item.getInt("series") - pos));
                play.setPageUrl(dataArray.getStr(item.getInt("resource") - pos));
                playItemList.add(play);
            }

            String lineTitle = dataArray.getStr(node.getInt("lineSourceName") - pos);
            LineInfo lineInfo = new LineInfo();
            lineInfo.setTitle(lineTitle);
            lineInfo.setPlayNumInfoList(playItemList);
            lineInfoList.add(lineInfo);
        }

        videoInfo.setLineInfoList(lineInfoList);
        return videoInfo;
    }

    @Override
    public String getPlayUrl(String url) {
        return url;
    }

    @Override
    public String getSearchUrl() {
        return LineUtil.getLineUtil().getLine() + "videoSearch?key=";
    }

    public List<Movie> search(String word) {
        String searchUrl = getSearchUrl() + word;
        nextUrl = null;
        return getMovies(searchUrl);
    }

    @Override
    public List<Movie> loadNext() {
        if (nextUrl == null) {
            return Collections.emptyList();
        } else {
            return getMovies(nextUrl);
        }
    }


}
